package com.baking.controller;

import com.baking.model.Role;
import com.baking.service.IRoleService;
import com.baking.util.Msg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.sql.Timestamp;
import java.util.List;

/**
 * @Author:wrb
 * @Date :2020/1/5/0:11
 * @Describtion:
 **/
@Controller
@RequestMapping("/role")
public class RoleController {

    @Autowired
    IRoleService iRoleService;

    @RequestMapping("/index")
    public String getRolePage(){
        return "/role/index";
    }

    @RequestMapping("/addPage")
    public String addPage(){
        return "/role/addPage";
    }

    @RequestMapping("/testPage")
    public String testPage(){
        return "/test/testPage";
    }


    @RequestMapping("/getRoleList")
    @ResponseBody
    public Msg getRoleList(){
        Msg msg = new Msg();
        Role role = new Role();
        List<Role> roleList = iRoleService.getRoleList(role);
        msg.setCode("0");
        msg.setData(roleList);
        return msg;
    }

    @RequestMapping(value = "/getRoleList_V2")
    @ResponseBody
    //public Msg getRoleList_V2(@RequestParam(value = "name",required = false,defaultValue = "") String name){
    public Msg getRoleList_V2(HttpServletRequest request){
        String result = "?后面的参数为：" + request.getQueryString();
        Msg msg = new Msg();
        Role role = new Role();
        System.out.println("getRoleList_V2 = [" + role + "]");
        List<Role> roleList = iRoleService.getRoleList(role);
        msg.setCode("0");
        msg.setData(roleList);
        return msg;
    }


    @RequestMapping("/getRoleById")
    @ResponseBody
    public Msg getRole(Role role){
        System.out.println("getRoleById = " + role );
        Msg msg = new Msg();
        List<Role> roleList = iRoleService.getRole(role);
        msg.setCode("0");
        msg.setData(roleList);
        return msg;
    }

    @RequestMapping(value = "/updateById",method = RequestMethod.POST)
    @ResponseBody
    public Msg updateById(Role role){
        Msg msg = new Msg();
        role.setUpdateDate(new Timestamp(System.currentTimeMillis()));
        System.out.println("role = [" + role + "]");
        boolean isUpdate = iRoleService.updateById(role);
        if(isUpdate){
            msg.setMsg("更新成功");
        }else {
            msg.doError("更新失败");
        }
        return msg;
    }

    @RequestMapping("/addRole")
    @ResponseBody
    public Msg addRole(Role role){
        Msg msg = new Msg();
        role.setInsertDate(new Timestamp(System.currentTimeMillis()));
        boolean isAdd = iRoleService.addRole(role);
        if(isAdd){
            msg.setMsg("新增成功");

        }else {
            msg.doError("新增失败");
        }
        return msg;
    }

    @RequestMapping("/deletRole")
    @ResponseBody
    public Msg deletRole(Role role){
        Msg msg = new Msg();
        boolean isDel = iRoleService.deletRole(role);
        if (isDel){
            msg.deSuccess("删除成功");
        }else msg.doError("删除失败");
        return msg;
    }
}
